<template>
  <!-- 列表 -->
  <ContentWrap>
    <div>酒类参数配置</div>
    <el-divider />
    <el-form class="-mb-15px" :model="formData" ref="queryFormRef" :inline="true" label-width="108px">
      <el-form-item label="默认审核" prop="audit">
        <el-switch :value="formData.audit" inline-prompt size="large" active-text="是" inactive-text="否"
          :inactive-value="0" :active-value="1" @input="auditChange(formData.audit)" />
      </el-form-item>
      <el-form-item label="默认保费比例" prop="ratio">
        <el-input-number v-model="formData.premiumRatio" placeholder="请输入默认比例" clearable :min="0" :max="100" class="!w-240px" />&nbsp;&nbsp;&nbsp;%
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" plain @click="submitForm()">
          保存配置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>

<script setup lang="ts">
import { ConfigApi, ConfigVO } from "@/api/wine/config";
// import {DICT_TYPE,getIntDictOptions} from "@/utils/dict";
/** 平台配置 列表 */
defineOptions({ name: "Config" });

const { t } = useI18n() // 国际化
const message = useMessage(); // 消息弹窗

const formData = ref({
  id: undefined,
  premiumRatio: 0,
  audit: 0,
})

const list = ref();

const auditChange = async (e) => {
  console.log(e, list.value.id);
  try {
    // 关闭的二次确认
    await message
      .confirm(`是否确认${e == 0 ? "打开" : "关闭"}审核操作？`)
      .then(async (res) => {
        console.log(res);
        formData.value.audit = formData.value.audit == 0 ? 1 : 0;
        //发起关闭
        await ConfigApi.updateConfig({ id: list.value.id, isAudit: formData.value.audit });
        message.success(`${e == 0 ? "打开" : "关闭"}成功`);
        // 刷新列表
        await getList();
      })
      .catch((err) => {
        console.log(err);
      });
  } catch { }
};

// const validateInput = (it) => {
//   console.log("value", it);
//   it = it + ''.replace(/[^0-9]/g, "");
//   if (it.indexOf(0) == 0) {
//     it = 0;
//   }
//   formData.value.premiumRatio = parseInt(it);
// }

/** 查询列表 */
const getList = async () => {
  try {
    const data = await ConfigApi.getConfigList({});
    if (data != null) {
      formData.value.id= data.id;
      formData.value.audit = data.isAudit;
      formData.value.premiumRatio = data.premiumRatio;
    }
    console.log(formData.value.premiumRatio,1111)
    list.value = data;
  } finally {
  }
};

/** 添加/修改操作 */
const submitForm = async () => {
  try {
    const data = formData.value as unknown as ConfigVO
    await ConfigApi.updateConfig(data)
    message.success(t('common.updateSuccess'))
  } finally {

  }
}
// const getIntDict=async()=>{
//  try {
//     const data = await getIntDictOptions(DICT_TYPE.IS_AUDIT)

//   } finally {
//   }
// }

/** 初始化 **/
onMounted(() => {
  getList();
  // getIntDict()
});
</script>
